//
//  ActivityViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/5.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class ActivityViewController: BaseViewController {

    @IBOutlet var titleTF: UITextField!
    
    @IBOutlet var contentTF: UITextField!
    
    @IBOutlet var editBtn: UIButton!
    
    var activityModels:[ActivityModel]?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        requestGetActivityInfo()
    }

    @IBAction func editBtnPress(_ sender: UIButton) {
        if activityModels != nil {
            if (activityModels?.count)! > 0 {
                let model = activityModels![0]
                requestEditActivityInfo(title: titleTF.text!, content: contentTF.text!,activityId:model.id)
            }
        }else {
             requestAddActivityInfo(title: titleTF.text!, content: contentTF.text!)
        }
        
    }
    
    
    //网络请求
    //添加活动信息
    func requestAddActivityInfo(title:String,content:String) -> Void {
        Alamofire.request(addActivityInfoUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String(),"uid":UserModel.shareUser.data?.uid ?? String(),"title":title,"content":content])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                    hud?.mode = MBProgressHUDMode.text
                    hud?.labelText = "添加成功"
                    hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                    }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                    })
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    
    //获取活动信息
    func requestGetActivityInfo() -> Void {
        Alamofire.request(getActivityInfoUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String()])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    var basemodel = BaseResponseOfModel<BaseArrayData<ActivityModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!

                    switch basemodel.status! {
                    case true:
                        print("获取成功")
                    self.activityModels = basemodel.data?.data
                        if (self.activityModels != nil){
                            
                            if (self.activityModels?.count)! > 0 {
                                let model = self.activityModels![0]
                                self.titleTF.text = model.title
                                self.contentTF.text = model.content
                            }
                        }
                        
                    
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    //编辑活动信息
    func requestEditActivityInfo(title:String,content:String,activityId:String) -> Void {
        Alamofire.request(editActivityInfoUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String(),"uid":UserModel.shareUser.data?.uid ?? String(),"title":title,"content":content,"id":activityId])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                    hud?.mode = MBProgressHUDMode.text
                    hud?.labelText = "编辑成功"
                    hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                    }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                    })
                    
                case false:
                    print(response.result.error!)
                }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
